/**
 * Replaces trailing zeros in an array with null.
 * @param {number[]} arr - The input array.
 * @returns {number[]} - The array with trailing zeros replaced by null.
 */
const replaceTrailingZerosWithNull = arr => {
    const lastNonZeroIndex = [...arr].reverse().findIndex(num => num !== 0)
    const replaceIndex = lastNonZeroIndex === -1 ? 0 : arr.length - lastNonZeroIndex
    return [...arr.slice(0, replaceIndex), ...new Array(arr.length - replaceIndex).fill(null)]
}

const formatTimeComponents = date => {
    const year = String(date.getFullYear())
    const month = String(date.getMonth() + 1).padStart(2, '0')
    const day = String(date.getDate()).padStart(2, '0')
    const hour = String(date.getHours()).padStart(2, '0')
    const minute = String(date.getMinutes()).padStart(2, '0')
    const second = String(date.getSeconds()).padStart(2, '0')

    return {
        year,
        month,
        day,
        hour,
        minute,
        second
    }
}

/**
 * Adjust the given date by adding (or subtracting if negative) a specified number of days.
 * If the adjusted date is after today and the third parameter is true, return today's date.
 *
 * @param {string | Date} startDateStr - The date to be adjusted, in the format "yyyy-mm-dd".
 * @param {number} daysToAdjust - The number of days to add (or subtract if negative) to the date.
 * @param {boolean} limitToToday - If true and the adjusted date is after today, return today's date.
 * @returns {string} - The adjusted date in the format "yyyy-mm-dd".
 */
const adjustDateByDays = (startDateStr, daysToAdjust = 0, limitToToday = false) => {
    let date = new Date(startDateStr)
    date.setDate(date.getDate() + daysToAdjust)
    if (limitToToday) {
        const today = new Date()
        today.setHours(0, 0, 0, 0)
        if (date > today) {
            date = today
        }
    }
    const { year, month, day } = formatTimeComponents(date)
    return `${year}-${month}-${day}`
}
//随机颜色
const getRandomColor= ()=> {  
    var letters = '0123456789ABCDEF';  
    var color = '#';  
    var exclude = ['#000000', '#FFFFFF']; // 排除黑白  
    var mainColors = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF']; // 推荐的主流颜色  
    var randomIndex = Math.floor(Math.random() * mainColors.length); // 随机选择一个主流颜色  
    color = mainColors[randomIndex]; // 设置颜色值  
      
    // 检查是否需要排除颜色  
    if (exclude.includes(color)) {  
        // 如果需要排除，则重新随机选择一个颜色  
        return getRandomMainColor();  
    }  
      
    return color;  
}  
export { formatTimeComponents, adjustDateByDays, replaceTrailingZerosWithNull,getRandomColor }
